<?php
declare(strict_types = 1);
namespace plugin\banmichaoqiangbizhi\api;

use app\banmi\common\Basehome;
use think\facade\Db;
use think\facade\Log;

class Index extends Basehome
{
    public $NotLogin =["plugin_cn","shop_tags","search_key","shop_search","index","shop_list","ViewAjax"];
    
    
    public function plugin_cn()
    {
        return 'banmichaoqiangbizhi';
    }
    //tag 标签
    public function shop_tags()
    {
        $controller=  $this->plugin_cn();
        $paginate=20;
        $tag_list =  Db::name('plugin_banmi_'.$controller.'_tag')->where('state', 2)->order('sort', 'asc')->paginate($paginate);
        $data['list']= $tag_list;
        ajaxmsg('ok', 200, $data);
    }
    //搜索标签
    public function search_key()
    {
        $controller=  $this->plugin_cn();
        $paginate=20;
        $tag_list =  Db::name('plugin_banmi_'.$controller.'_search_key')->where('state', 2)->order('sort', 'asc')->paginate($paginate);
        $data['list']= $tag_list;
        ajaxmsg('ok', 200, $data);
    }
    public function shop_search()
    {
        $controller=  $this->plugin_cn();
        $arr = $_POST;
   
        $paginate=20;
        $lat=  input('request.lat', '');//39.897528
        $lng=  input('request.lng', '');//116.330742
        $keyword=  input('request.keyword', '');
        if (!$keyword) {
            ajaxmsg('请输入关键词', 0);
        }
        $where[] = ['status', '=', 2];
   
        $where[] = ['key_name', 'like', "%{$keyword}%"];
 
        
        $list =  Db::name('plugin_banmi_'.$controller.'_list')->where($where)->paginate($paginate);

        
        
        $data['shop_list']= $list;
        ajaxmsg('ok', 200, $data);
    }
  
    
    public function Shop_my_shop_del()
    {
        $uid = $this->uid;
        $id=input('request.id');
        $map['id']=$id;
        $map['uid']=$uid;
           
            
        Db::name('plugin_banmi_'.$this->plugin_cn().'_list')->where($map)->update(['status' => 1 ]);
            
        ajaxmsg('删除成功', 200);
    }
    public function Shop_my_shop()
    {
        $arr = $_POST;
        $uid = $this->uid;
        $paginate=20;
        $lat=  input('request.lat', '');//39.897528
        $lng=  input('request.lng', '');//116.330742
        
        $map['uid']=$uid;
       
        $list =  Db::name('plugin_banmi_'.$this->plugin_cn().'_list')->where($map)->order('id', 'desc')->where('status', '>=', '0')
        
        ->paginate($paginate);
     
     
        
        
        $data['list']= $list;
        ajaxmsg('ok', 200, $data);
    }
    public function Shop_my_collection_del()
    {
        $uid = $this->uid;
        $id=input('request.id');
        $map['id']=$id;
        $map['uid']=$uid;
        Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->where($map)->delete();
        ajaxmsg('删除成功', 200);
    }
    public function Shop_my_collection()
    {
        $arr = $_POST;
        $uid = $this->uid;
 
        $paginate=20;

        
        $map['a.uid']=$uid;
        $map['b.status']=2;
        $list =  Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->alias('a')
                ->join('plugin_banmi_'.$this->plugin_cn().'_list b', 'a.shop_id = b.id')
        
        ->where($map)->field('a.id,a.shop_id,b.shop_name,b.shop_img,b.uid,b.shop_tag')->paginate($paginate);
    
   
        
        
        $data['list']= $list;
        ajaxmsg('ok', 200, $data);
    }
    public function my_download()
    {
        $arr = $_POST;
        $uid = $this->uid;
 
        $paginate=20;
 
        
        $map['a.uid']=$uid;
        $map['b.status']=2;
        $list =  Db::name('plugin_banmi_'.$this->plugin_cn().'_download')->alias('a')
                ->join('plugin_banmi_'.$this->plugin_cn().'_list b', 'a.shop_id = b.id')
        
        ->where($map)->field('a.id,a.shop_id,b.shop_name,b.shop_img,b.uid,b.shop_tag')->paginate($paginate);

        
        $data['list']= $list;
        ajaxmsg('ok', 200, $data);
    }
    
    public function ShopAddajax()
    {
        $arr = $_POST;
        $uid =$this->uid;

        if (empty($arr['nav_id'])) {
            $result['msg'] = '请选择分类！';
            ajaxmsg($result['msg'], 0);
        }
        if (empty($arr['shop_img'])) {
            $result['msg'] = '请上传图片！';
            ajaxmsg($result['msg'], 0);
        }
        $arr['shop_tag'] = str_replace("，", ",", $arr['shop_tag']);
        $set_json = plugin_config(0);
       
        $status=$set_json['is_check_examine'];
        
        
        if (isset($arr['id'])&&$arr['id']>1) {
            $find=   Db::name('plugin_banmi_banmichaoqiangbizhi_list')->where('id', $arr['id'])->where('uid', $uid)->find();
            if (!$find) {
                ajaxmsg('非法操作1', 0);
            }
            $up_data = array(
            'shop_name'     => $arr['shop_name'],
            'shop_img'      => $arr['shop_img'],
            'shop_tag'      => $arr['shop_tag'],
            'nav_id'        => $arr['nav_id'],
            'key_name'      => $arr['shop_name']. $arr['shop_tag'],
            'status'        => $status
           );
           
            Db::name('plugin_banmi_banmichaoqiangbizhi_list')
              ->where('id', $arr['id'])
              ->update($up_data);
           
            ajaxmsg('编辑成功,正在跳转', 200);
        }
        
       
        
        
        
        
    
        $insert_data = array(
            'shop_name'     => $arr['shop_name'],
            'shop_img'      => $arr['shop_img'],
            'shop_tag'      => $arr['shop_tag'],
            'creation_time' => time(),
            'creation_ip'   => request()->ip(),
            'uid'           => $uid,
            'nav_id'        => $arr['nav_id'],
             'key_name'      => $arr['shop_name']. $arr['shop_tag'],
            'status'        => $status
           );
        
  
 
        $result =  Db::name('plugin_banmi_'.$this->plugin_cn().'_list')->insert($insert_data);
        if (!empty($result)) {
            ajaxmsg('投稿成功,正在跳转', 200);
        }
        
        ajaxmsg('失败！', 0);
    }
    
    
    
    
    
    public function shop_edit()
    {
        $uid = $this->uid;
        $id =    input('request.id');
     
        $NavList = Db::name('plugin_banmi_'.$this->plugin_cn().'_nav')->select();
        $find =  Db::name('plugin_banmi_'.$this->plugin_cn().'_list')->where('status', '>=', '0')
        ->where('uid', $uid)->where('id', $id)->find();
        $data['NavList'] = $NavList;
        $data['find'] = $find;
   
        ajaxmsg('ok', 200, $data);
    }
    
    public function index()
    {
        $controller=  $this->plugin_cn();
   
   
        $NavList = Db::name('plugin_banmi_'.$controller.'_nav')->select();
        $NavList = $NavList->all();
 

 
  
    
        $data['NavList'] = $NavList;
        ajaxmsg('ok', 200, $data);
    }

    public function shop_list()
    {
        $controller=  $this->plugin_cn();
        $nav_id =    input('request.nav_id');
        $keyword =   input('request.keyword');
        $tabindex =  input('request.tabindex', 1);
        $lat=  input('request.lat');//39.897528
        $lng=  input('request.lng');//116.330742
        $tag_name =   input('request.tag_name');
 
        $paginate=20;
        $field = 'id';
        if ($tabindex == 0) {
            $field = 'sort';
        }
        $page = 1;
        $map['status']=2;
        $where = "status=2";
        $NavList = Db::name('plugin_banmi_'.$controller.'_nav')->select();
        $data['NavList']=$NavList;
        
      
        if ($nav_id > 0) {
            $map['nav_id']=$nav_id;
            $where.= " and nav_id= " . $nav_id;
        }
        if (!empty($keyword)) {
            $where.= " and shop_name like '%{$keyword}%'";
        }
        if (!empty($tag_name)) {
            $where.= " and shop_tag like '%{$tag_name}%'";
        }
        $list=  Db::name('plugin_banmi_'.$controller.'_list')->where($where) ->order($field.' desc')->paginate($paginate);
   
       
   
        $data['shop_list']=$list;
        ajaxmsg('ok', 200, $data);
    }
    
    public function ViewAjax()
    {
        $id =  input('request.id');
        $find =  Db::name('plugin_banmi_'.$this->plugin_cn().'_list')->where('id', $id)->where('status', 2)->find();
        
        if (empty($find)) {
            ajaxmsg('内容不存在,或审核中', 0);
        }
        $find['is_collection'] = 0;
        $uid = $this->uid;
      
      
 

        if ($uid) {
            $is_find =   Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->where(['shop_id'=>$find['id'],'uid'=>$uid])->find();
            if ($is_find) {
                $find['is_collection'] = 1;
            }
        }
   
        $data['find'] = $find;
        ajaxmsg('ok', 200, $data);
    }
     
    public function collection_add()
    {
        $uid = $this->uid;
        $shop_id = input('request.shop_id');
        if (empty($shop_id)) {
            ajaxmsg('非法操作', 0);
        }
        $map['uid']=$uid;
        $map['shop_id']=$shop_id;
        $is_find =  Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->where($map)->find();
        
        if (!empty($is_find)) {
            $is_del =   Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->delete($is_find['id']);
            ajaxmsg('已取消收藏', 100);
        }
        $result =  Db::name('plugin_banmi_'.$this->plugin_cn().'_collection')->insert([
            'uid' => $uid,
            'shop_id' => $shop_id ,
            'creation_time' => time() ,
            ]);
        ajaxmsg('收藏成功', 200);
    }
}
